import 'package:aero_frisbee/app/model/af_block_list_model/af_block_list_model.dart';
import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:aero_frisbee/pages/modues/message/pages/followed/view_model/af_followed_list_view_model.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'view_model/af_followed_list_state.dart';
import 'views/af_followed_list_cell.dart';

class AfFollowedListScreen extends ConsumerStatefulWidget {
  const AfFollowedListScreen({super.key});
  @override
  ConsumerState<ConsumerStatefulWidget> createState() =>
      _AfFollowedListScreenState();
}

class _AfFollowedListScreenState extends ConsumerState<AfFollowedListScreen> {
  final EasyRefreshController _controller = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );
  @override
  Widget build(BuildContext context) {
    ref.listen(afFollowedListServiceProvider, (previousData, newData) {
      final state = newData.maybeWhen(
        data: (data) => data,
        orElse: () => null,
      );
      if (state != null) {
        state.refreshState.maybeWhen(
          refreshSuccess: (hasMore) {
            _controller.finishRefresh();
            _controller.resetFooter();
            _controller.finishLoad(
                hasMore ? IndicatorResult.success : IndicatorResult.noMore);
          },
          loadMoreSuccess: (hasMore) {
            _controller.finishLoad(
                hasMore ? IndicatorResult.success : IndicatorResult.noMore);
          },
          loadMoreErr: (error) {
            _controller.finishLoad(IndicatorResult.fail);
          },
          refreshErr: (error) {
            _controller.finishRefresh(IndicatorResult.fail);
            _controller.resetFooter();
          },
          orElse: () {},
        );
      }
    });

    final afFollowedListProviderState =
        ref.watch(afFollowedListServiceProvider);
    final blockList = ref
        .watch(getBlockListFutureProvider)
        .maybeWhen(
          orElse: () => List<AfBlockListModel>.from([]),
          data: (data) => data,
        )
        .map((e) => e.broadcasterId ?? '')
        .toList();
    return SafeArea(
      child: afFollowedListProviderState.maybeWhen(
        orElse: () => Container(),
        loading: () => const Center(child: CircularProgressIndicator()),
        data: (data) {
          return _buildListView(data, blockList);
        },
      ),
    );
  }

  _buildListView(AfFollowedListState state, List<String> blockList) {
    if (state.dataList.isEmpty) {
      return Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
        ),
        padding: const EdgeInsets.all(20),
        child: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Assets.icons.profileNoData.image(width: 150),
              ),
              Text(
                'No Data',
                style: TextStyle(
                  color: const Color(0xff000000).withOpacity(0.3),
                  fontWeight: FontWeight.normal,
                  fontSize: 16,
                ),
                textAlign: TextAlign.left,
                overflow: TextOverflow.ellipsis,
                maxLines: 1,
              ),
              const SizedBox(
                height: 120,
              ),
            ],
          ),
        ),
      );
    }
    final retList = state.dataList
        .where(
          (e) => !blockList.contains(e.userId ?? ''),
        )
        .toList();
    return EasyRefresh(
      controller: _controller,
      refreshOnStart: false,
      onRefresh: () {
        ref.read(afFollowedListServiceProvider.notifier).refresh();
      },
      onLoad: () {
        ref.read(afFollowedListServiceProvider.notifier).loadMore();
      },
      child: ListView.separated(
        padding: const EdgeInsets.only(top: 0),
        itemBuilder: (context, index) {
          final item = retList[index];
          return AfFollowedListCell(
            item: item,
          );
        },
        separatorBuilder: (context, index) => const SizedBox(
          height: 0,
        ),
        itemCount: retList.length,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(AfFollowedListScreen oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
